import org.eclipse.swt.widgets.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class TeamEditor extends ItemEditor<Team>
{
    private static String[] defaultTeamNames
            = { "alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta",
                "theta", "iota", "kappa", "lambda", "mu", "nu", "xi", "omicron",
                "pi", "rho", "sigma", "tau", "upsilon", "phi", "chi", "psi", "omega"};

    private static int nextDefaultIndex = 0;

    private String getDefaultName()
    {
        String out;
        if (nextDefaultIndex < defaultTeamNames.length)
        {
            out = defaultTeamNames[nextDefaultIndex];
        }
        else out = "team " + nextDefaultIndex;
        nextDefaultIndex++;

        return out;
    }

    public TeamEditor(Judicator k, Shell parent, String title, Team target)
    {
        super(k, parent, title, target, 520, 480);
    }

    private List students, inTeam;
    private Label studentsLabel, inTeamLabel, nameLabel;
    private Text name;
    private Button include, exclude;

    protected void _init()
    {
        students = new List(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
        inTeam = new List(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
        studentsLabel = new Label(shell, SWT.SHADOW_OUT);
        inTeamLabel = new Label(shell, SWT.SHADOW_OUT);
        nameLabel = new Label(shell, SWT.SHADOW_OUT);
        name = new Text(shell, SWT.BORDER);
        include = new Button(shell, SWT.PUSH);
        exclude = new Button(shell, SWT.PUSH);

        nameLabel.setBounds(20, 25, 70, 25);
        nameLabel.setText("team name:");

        name.setBounds(100, 25, 300, 25);
        name.setEditable(true);

        studentsLabel.setBounds(20, 80, 50, 30);
        studentsLabel.setText("students:");

        inTeamLabel.setBounds(300, 80, 50, 30);
        inTeamLabel.setText("team:");

        students.setBounds(20, 110, 200, 340);
        inTeam.setBounds(300, 110, 200, 340);

        ok.setBounds(175,480,60,30);
        cancel.setBounds(285,480,60,30);

        include.setBounds(235, 250, 50, 50);
        include.setText(">");
        include.addSelectionListener(new SelectionAdapter()
        {   public void widgetSelected(SelectionEvent e) {includeStudents();}});

        exclude.setBounds(235, 320, 50, 50);
        exclude.setText("<");
        exclude.addSelectionListener(new SelectionAdapter()
        {   public void widgetSelected(SelectionEvent e) {excludeStudents();}});

        if (target == null) target = new Team();

        for (int i = 0; i < judicator.students.size(); i++)
        {
            Student s = judicator.students.get(i);
            if (!target.students.contains(s))
            {
                if (s.team == null)
                {
                    students.add(s.name);
                }
                else
                {
                    students.add(s.name + " [" + s.team.name + "]");
                }
            }
        }

        name.setFocus();
    }

    protected void _assumeData()
    {
        name.setText(target.name);
        for (int i = 0; i < target.students.size(); i++)
        {
            Student s = target.students.get(i);
            if (s.team == null)
            {
                inTeam.add(s.name);
            }
            else
            {
                inTeam.add(s.name + " [" + s.team.name + "]");
            }
        }
    }

    private void copySettings()
    {
        if (name.getText().length() > 0) target.name = name.getText();
        else target.name = getDefaultName();

        for (int i = 0; i < target.students.size(); i++)
        {
            target.students.get(i).team = null;
        }

        target.students.clear();
        String[] names = inTeam.getItems();
        for (int i = 0; i < names.length; i++)
        {
            String stName;
            if (names[i].contains("["))
            {
                stName = names[i].substring(0,names[i].indexOf("[") - 1);
            }
            else stName = names[i];

            Student s = judicator.getStudent(stName);

            if (s.team != target)
            {
                target.students.add(s);
                if (s.team != null) judicator.removeFromTeam(s);
                s.team = target;
            }
        }
    }

    protected void _addItem()
    {
        copySettings();
        judicator.addTeam(target);
        close();
    }

    protected void _modifyItem()
    {
        copySettings();
        judicator.update(target);
        close();
    }

    protected void _close()
    {
        judicator.mainWindow.teamEditor = null;
    }

    private void includeStudents()
    {
        String[] names = students.getSelection();

        for (int i = 0; i < names.length; i++)
        {
            students.remove(names[i]);
            inTeam.add(names[i]);
        }
    }

    private void excludeStudents()
    {
        String[] names = inTeam.getSelection();

        for (int i = 0; i < names.length; i++)
        {
            inTeam.remove(names[i]);
            students.add(names[i]);
        }
    }
}
